"""文件读取"""


def read_pi():
    # 通过open和close来读取文件
    """
    file_object = open("pi_30_digits.txt")
    contents = file_object.read().rstrip()
    print(contents)
    file_object.close()
    print("------end-------")
    """

    # 通过with...as..语句来读取文件
    with open("pi_30_digits.txt", encoding="UTF-8") as file_object:
        contents = file_object.read()
        print(contents.rstrip())
        print("------end-------")

    # 指定文件读取的模式
    with open("pi_30_digits.txt", "rb") as file_object:
        contents = file_object.read()
        print(contents.rstrip())
        print("------end-------")

    # 逐行读取
    with open("pi_30_digits.txt", encoding="UTF-8") as file_object:
        for line_str in file_object:
            print(line_str.rstrip())
        print("------end-------")

    # 逐行读取
    try:
        with open("12.txt", encoding="UTF-8") as file_object:
            lines = file_object.readlines(0)
    except IOError as e:
        print("程序读写异常:", e)
    except FileNotFoundError as e:
        print("文件不存在", e)
    else:
        for temp in lines:
            print(temp.rstrip())
        print("------end-------")


read_pi()
